<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
class General_Model extends Model 
{
    function General_Model()
    {
        // Call the Model constructor
        parent::Model();
    }
	
// SELECT =================================

	function get_all_user($id,$thedate=0,$num, $offset)
	{
		if($id!='all')
		{
			$sql = "SELECT * FROM news where id <>".$id." and thedate <='".$thedate."' order by  thedate desc limit 8 ";
			$query = $this->db->query($sql);
			if ($query->num_rows() <= 0) 
			{
			$sql = "SELECT * FROM news where id <>".$id." and thedate >='".$thedate."' order by  thedate desc limit 8 ";
			$query = $this->db->query($sql);			
			}
		}
		else
		{
			//$sql = "SELECT * FROM news order by thedate desc";
			$this->db->limit($num, $offset);
			$this->db->order_by("thedate", "desc"); 
			$query = $this->db->get('news');
		}
		return $query->result();
	}

	function get_user_detail($id)
	{

		$sql = "SELECT * FROM news where id=".$id;
		$query = $this->db->query($sql);
		
		// update pageview of news
		$sqlpageview = "update news set pageview=pageview+1 where id=".$id;
		$this->db->query($sqlpageview);
		return $query->result();
	}
	function get_slideshow()
	{
		$this->db->select('image');
		$this->db->from('slideshow');
		$this->db->order_by("sorting", "asc");
		$query = $this->db->get();		
		return $query;
	} 
	
// INSERT =================================

	function insert_new_user($info)
	{
		$this->db->insert('ua_users', $info); 
	}
	
// UPDATE =================================

	function update_user($id)
	{

		$sql = "SELECT * FROM news where id=".$id;
		$query = $this->db->query($sql);
		
		// update pageview of news
		$sqlpageview = "update news set pageview=pageview+1 where id=".$id;
		$this->db->query($sqlpageview);
		return $query->result();
	}
	
// DELETE =================================

	function delete_user($id)
	{

		$sql = "SELECT * FROM news where id=".$id;
		$query = $this->db->query($sql);
		
		// update pageview of news
		$sqlpageview = "update news set pageview=pageview+1 where id=".$id;
		$this->db->query($sqlpageview);
		return $query->result();
	}
	
// FUNCTION MANIPULATION	

	function check_user($name)
	{

		$sql = "SELECT * FROM news where id=".$id;
		$query = $this->db->query($sql);
		
		// update pageview of news
		$sqlpageview = "update news set pageview=pageview+1 where id=".$id;
		$this->db->query($sqlpageview);
		return $query->result();
	}
	
	function get_support()
	{
		
		$langid=$this->session->userdata['langid'];
		$this->db->select('*');
		$this->db->from('support_description');
		$this->db->join('support', 'support.id = support_description.support_id');
		$this->db->order_by("support.id", "asc");
		$this->db->where('language_id', $langid);
		$query = $this->db->get();		
		return $query;
	}
	
	function get_friend_link()
	{
		$query = $this->db->get('friend_link');
		return $query;
	}
	function get_product_menu()
	{
		$langid=$this->session->userdata['langid'];
		$this->db->select('*');
		$this->db->from('category_description');
		$this->db->join('category', 'category.category_id = category_description.category_id');
		$this->db->order_by("category.sort_order", "asc");
		$this->db->where('category_type_id', '1');
		$this->db->where('language_id', $langid); 
		$query = $this->db->get();
		//$query = $this->db->get('friend_link');
		return $query;
	}	

	function get_sub_menu($opositelang,$current_menu)
	{		
		$sql="SELECT name_alias FROM category_description where language_id=".$opositelang." and category_id IN ( SELECT category_id FROM category_description WHERE name_alias LIKE '".$current_menu."' )";
		$query = $this->db->query($sql);
		return $query;
	}	
	
	function get_title_website($table,$field)
	{		
		$sql="SELECT name FROM ".$table." where ".$field." LIKE '".$current_menu."'";
		$query = $this->db->query($sql);
		return $query;
	}	

	function get_product_sub_menu($current_menu)
	{
		$langid=$this->session->userdata['langid'];
		$sql='SELECT product_description.namepro_alias,product_description.name, product.image, category_description.name as sub_title FROM category_description ,product,product_description,product_to_category WHERE category_description.category_id=product_to_category.category_id and category_description.name_alias like "'.$current_menu.'" and product_description.language_id ='.$langid.' and product.product_id =product_description.product_id and product.product_id=product_to_category.product_id';
		$query = $this->db->query($sql);
		return $query;


	}
	function get_new_product()
	{
		$langid=$this->session->userdata['langid'];
		$this->db->select('*');
		$this->db->from('product_description');
		$this->db->join('product', 'product.product_id = product_description.product_id');
		$this->db->order_by("product.sort_order", "asc");
		$this->db->limit(4);
		$this->db->where('new_product', '1');
		$this->db->where('language_id', $langid); 
		$query = $this->db->get();
		//$query = $this->db->get('friend_link');
		return $query;
	}
	
	function get_new_tips()
	{
		$langid=$this->session->userdata['langid'];
		$this->db->select('*');
		$this->db->from('article_description');
		$this->db->join('article', 'article.id = article_description.article_id');
		$this->db->limit(5);
		$this->db->where('category_id', '9');
		$this->db->where('language_id', $langid); 
		$query = $this->db->get();
		//$query = $this->db->get('friend_link');
		return $query;
	}	
	
	function get_foreword()
	{
		$langid=$this->session->userdata['langid'];
		$this->db->select('*');
		$this->db->from('information_description');
		$this->db->where('information_id', '3');
		$this->db->where('language_id', $langid); 
		$query = $this->db->get();
		//$query = $this->db->get('friend_link');
		return $query;
	}	
	
	function get_hot_news()
	{
		$langid=$this->session->userdata['langid'];
		$this->db->select('*');
		$this->db->from('article_description');
		$this->db->join('article', 'article.id = article_description.article_id');
		$this->db->limit(5);
		$this->db->order_by("article.thedate", "desc");
		$id = array('5', '6');
		$this->db->where_in('category_id', $id);
		$this->db->where('language_id', $langid); 
		$query = $this->db->get();
		//$query = $this->db->get('friend_link');
		return $query;
	}	
	
		
	

}
